<?php
namespace frontend\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class MiniLoginForm extends Model
{
    public $mobile;
    public $password;
    public $code;
    public $rememberMe = false;

    private $_user;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['mobile', 'required','on' => ['default','login_sms_code']],
            ['mobile', 'integer','on' => ['login_sms_code']],
            ['mobile','match','pattern'=>'/^1[0-9]{10}$/','on' => ['default','login_sms_code'],'message'=>'{attribute}必须为1开头的11位纯数字'],
            ['mobile', 'string', 'min'=>11,'max' => 11,'on' => ['default','login_sms_code']],
            ['password', 'required'],
            ['code', 'integer','on' => ['default','login_sms_code']],
            ['code', 'string', 'min'=>6,'max' => 6,'on' => ['default','login_sms_code'],'message'=>'请输入6位数验证码'],
            //['code', 'required','requiredValue' => (string) $this->getSmsCode(),'on' => ['default','login_sms_code'],'message'=>'手机验证码输入错误'],
            //['code', 'validateCode'],
        ];
    }

    /**
     * Validates the code.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validateCode($attribute, $params)
    {
        $this->addError($attribute, '验证码错误.');
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            $session_code = $this->getSmsCode();
            if( $session_code != $this->code ){
                $this->addError($attribute, '验证码错误.');
            }
        }
    }

    public function getSmsCode()
    {
        //检查session是否打开
        if(!Yii::$app->session->isActive){
            Yii::$app->session->open();
        }
        //取得验证码和短信发送时间session
        $signup_sms_code = intval(Yii::$app->session->get('login_sms_code'));
        $signup_sms_time = Yii::$app->session->get('login_sms_time');

        $lefttime = time() - $signup_sms_time;
        //三分钟内有效
        if( $lefttime < 60*3 ){
            return (string) $signup_sms_code;
        }else{
            return 888888;
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    protected function getUser()
    {
        if ($this->_user === null) {
            $this->_user = User::findByMobile($this->mobile);
        }

        return $this->_user;
    }

    /**
     * @param $id
     * @return static
     */
    protected function getUserById( $id ){
        return User::findOne($id);
    }
}
